﻿<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
    xmlns:imaging="http://blogs.msdn.com/synergist/imaging"
    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
    xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:rels="http://schemas.openxmlformats.org/package/2006/relationships"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
    xmlns:w10="urn:schemas-microsoft-com:office:word"
    xmlns:util="urn:utilities"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    extension-element-prefixes="imaging util"
    exclude-result-prefixes="a imaging msxsl pic pkg r rels util v w wp xsl">

  <xsl:import href="VML2WPF.xslt"/>
  <xsl:import href="OMML2Xaml.xslt"/>

  <xsl:output encoding="utf-8" indent="yes" omit-xml-declaration="no" method="xml" standalone="yes"/>


  <xsl:param name="IsOptimalParagraphEnabled" select="'True'"/>
  <xsl:param name="IsHyphenationEnabled" select="'True'"/>
  <xsl:param name="IsColumnWidthFlexible" select="'False'"/>
  <xsl:param name="ColumnWidth" select="'Auto'"/>
  <xsl:param name="PagePadding" select="0"/>

  <xsl:template match="m:oMathPara">
    <InlineUIContainer>
      <xsl:apply-templates select="m:oMath"/>
    </InlineUIContainer>
  </xsl:template>

  <xsl:template match="/pkg:package">
    <xsl:comment>This WPF XAML was generated by Michael S. Scherotter's Word 2007 to Xaml Generator: http://www.codeplex.com/Word2007ToXaml</xsl:comment>
    <xsl:apply-templates select="pkg:part[@pkg:name='/word/document.xml']/pkg:xmlData/w:document">
      <xsl:with-param name="styles" select="pkg:part[@pkg:name='/word/styles.xml']/pkg:xmlData/w:styles"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="w:document">
    <xsl:param name="styles"/>
    <FlowDocument IsOptimalParagraphEnabled="{$IsOptimalParagraphEnabled}" IsHyphenationEnabled="{$IsHyphenationEnabled}" IsColumnWidthFlexible="{$IsColumnWidthFlexible}" ColumnWidth="{$ColumnWidth}" PagePadding="{$PagePadding}">
      <FlowDocument.Resources>

        <xsl:apply-templates select="$styles/w:style[not(w:basedOn/@w:val)]">
          <xsl:sort select="@w:styleId"/>
          <xsl:with-param name="styles" select="$styles"/>
        </xsl:apply-templates>
        <xsl:copy-of select="$VMLStyles"/>
      </FlowDocument.Resources>
      <xsl:apply-templates select="w:body/*"/>
    </FlowDocument>
  </xsl:template>

  <xsl:variable name="PtToPixels" select="96 div 72"/>
  <xsl:variable name="CharUnitsToPixels" select="96 * 0.25 div 720"/>

  <xsl:template match="w:style">
    <xsl:param name="styles"/>
    <xsl:comment>
      <xsl:value-of select="concat(w:name/@w:val, ' Style')"/>
    </xsl:comment>

    <xsl:variable name="basedOn" select="w:basedOn/@w:val"/>

    <Style x:Key="{@w:styleId}">
      <xsl:if test="$basedOn">
        <xsl:attribute name="BasedOn">
          {StaticResource <xsl:value-of select="$basedOn"/>}
        </xsl:attribute>
      </xsl:if>
      <xsl:attribute name="TargetType">
        <xsl:choose>
          <xsl:when test="@w:type='character'">Run</xsl:when>
          <xsl:when test="@w:type='numbering'">List</xsl:when>
          <xsl:when test="@w:type='paragraph'">Paragraph</xsl:when>
          <xsl:when test="@w:type='table'">Table</xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@w:type"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:if test="w:rPr/w:b">
        <Setter Property="FontWeight" Value="Bold"/>
      </xsl:if>
      <xsl:if test="w:rPr/w:color/@w:val and w:rPr/w:color/@w:val != 'auto'">
        <Setter Property="Foreground" Value="#{w:rPr/w:color/@w:val}"/>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="w:rPr/w:sz/@w:val">
          <Setter Property="FontSize"   Value="{$PtToPixels * w:rPr/w:sz/@w:val div 2}"/>
        </xsl:when>
        <xsl:when test="not($basedOn) and $styles/w:docDefaults/w:rPrDefault/w:rPr/w:sz/@w:val">
          <Setter Property="FontSize"   Value="{$PtToPixels * $styles/w:docDefaults/w:rPrDefault/w:rPr/w:sz/@w:val div 2}"/>
        </xsl:when>
      </xsl:choose>

      <!-- Text Alignment -->
      <xsl:if test="@w:type='paragraph'">
        <xsl:choose>
          <xsl:when test="w:pPr/w:jc/@w:val = 'left'">
            <Setter Property="TextAlignment" Value="Left"/>
          </xsl:when>
          <xsl:when test="w:pPr/w:jc/@w:val = 'center'">
            <Setter Property="TextAlignment" Value="Center"/>
          </xsl:when>
          <xsl:when test="w:pPr/w:jc/@w:val = 'right'">
            <Setter Property="TextAlignment" Value="Right"/>
          </xsl:when>
          <xsl:when test="w:pPr/w:jc/@w:val = 'justify'">
            <Setter Property="TextAlignment" Value="Justify"/>
          </xsl:when>
          <xsl:otherwise>
            <Setter Property="TextAlignment" Value="Left"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <xsl:choose>
        <xsl:when test="w:pPr/w:rPr/w:lang/@w:val">
          <Setter Property="Language" Value="{w:pPr/w:rPr/w:lang/@w:val}"/>
        </xsl:when>
        <xsl:when test="not($basedOn)">
          <Setter Property="Language" Value ="{$styles/w:docDefaults/w:rPrDefault/w:rPr/w:lang/@w:val}"/>
        </xsl:when>
      </xsl:choose>

      <!-- Font Family -->
      <xsl:choose>
        <xsl:when test="w:rPr/w:rFonts/@w:ascii">
          <Setter Property="FontFamily" Value="{w:rPr/w:rFonts/@w:ascii}"/>
        </xsl:when>
        <xsl:when test="w:rPr/w:rFonts/@w:asciiTheme='majorHAnsi'">
          <Setter Property="FontFamily" Value="{//a:theme/a:themeElements/a:fontScheme/a:majorFont/a:latin/@typeface}"/>
        </xsl:when>
        <xsl:when test="not($basedOn)">
          <Setter Property="FontFamily" Value="{//a:theme/a:themeElements/a:fontScheme/a:minorFont/a:latin/@typeface}"/>
        </xsl:when>
      </xsl:choose>

      <!-- Italic -->
      <xsl:if test="w:rPr/w:i">
        <Setter Property="FontStyle" Value="Italic"/>
      </xsl:if>

      <xsl:if test="@w:type='table' and w:tblPr/w:tblBorders">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness">
          <xsl:attribute name="Value">
            <xsl:choose>
              <xsl:when test="w:tblPr/w:tblBorders/w:left">
                <xsl:apply-templates select="w:tblPr/w:tblBorders/w:left" mode="Thickness"/>
              </xsl:when>
              <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
            <xsl:text>,</xsl:text>
            <xsl:choose>
              <xsl:when test="w:tblPr/w:tblBorders/w:top">
                <xsl:apply-templates select="w:tblPr/w:tblBorders/w:top" mode="Thickness"/>
              </xsl:when>
              <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
            <xsl:text>,</xsl:text>
            <xsl:choose>
              <xsl:when test="w:tblPr/w:tblBorders/w:right">
                <xsl:apply-templates select="w:tblPr/w:tblBorders/w:right" mode="Thickness"/>
              </xsl:when>
              <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
            <xsl:text>,</xsl:text>
            <xsl:choose>
              <xsl:when test="w:tblPr/w:tblBorders/w:bottom">
                <xsl:apply-templates select="w:tblPr/w:tblBorders/w:bottom" mode="Thickness"/>
              </xsl:when>
              <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
        </Setter>
      </xsl:if>
      <xsl:if test="w:pPr/w:ind/@w:left">
        <Setter Property="Margin" Value="{$CharUnitsToPixels * w:pPr/w:ind/@w:left},0,0,0"/>
      </xsl:if>

    </Style>

    <xsl:variable name="styleId" select="@w:styleId"/>
    <xsl:comment>
      Styles based on <xsl:value-of select="$styleId"/>:
    </xsl:comment>
    <xsl:apply-templates select="$styles/w:style[w:basedOn/@w:val=$styleId]">
      <xsl:sort select="@w:styleId"/>
      <xsl:with-param name="styles" select="$styles"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="w:del"/>

  <xsl:template name="Style">
    <xsl:param name="style"/>
    <xsl:if test="$style/w:rPr/w:b">
      <xsl:attribute name="FontWeight">Bold</xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:rPr/w:color/@w:val">
      <xsl:attribute name="Foreground">
        <xsl:value-of select="concat('#', $style/w:rPr/w:color/@w:val)"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:rPr/w:sz/@w:val">
      <xsl:attribute name="FontSize">
        <xsl:value-of select="$style/w:rPr/w:sz/@w:val div 2"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:rPr/w:rFonts/@w:ascii">
      <xsl:attribute name="FontFamily">
        <xsl:value-of select="$style/w:rPr/w:rFonts/@w:ascii"/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="w:tab">
    <Run Text=" "/>
  </xsl:template>

  <xsl:template match="w:p">
    <xsl:variable name="styleId">
      <xsl:choose>
        <xsl:when test="w:pPr/w:pStyle/@w:val">
          <xsl:value-of select="w:pPr/w:pStyle/@w:val"/>
        </xsl:when>
        <xsl:otherwise>
          <!-- The default paragraph style -->
          <xsl:value-of select="//w:style[@w:type='paragraph' and @w:default=1]/@w:styleId"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="style" select="//w:style[@w:styleId=$styleId]"/>

    <xsl:comment>
      Paragraph <xsl:value-of select="@w:anchor"/>
    </xsl:comment>
    <Paragraph Style="{{StaticResource {$styleId}}}">
      <xsl:if test="w:bookmarkStart">
        <xsl:attribute name="x:Name">
          <xsl:value-of select="generate-id(w:bookmarkStart/@w:name)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="w:pPr/w:jc/@w:val='center'">
        <xsl:attribute name="TextAlignment">Center</xsl:attribute>
      </xsl:if>
      <xsl:if test="w:pPr/w:rPr/w:lang/@w:val">
        <xsl:attribute name="Language">
          <xsl:value-of select="w:pPr/w:rPr/w:lang/@w:val"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="*"/>
    </Paragraph>
  </xsl:template>

  <xsl:template match="@w:val" mode="Style">
    <xsl:attribute name="Style">
      <xsl:value-of select="concat('{StaticResource ', ., '}')"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="w:r">
    <xsl:call-template name="TextStyle">
      <xsl:with-param name="style" select="w:rPr/node()[1]"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="w:r[w:pict]">
    <Figure>
      <xsl:apply-templates select="*"/>
    </Figure>
  </xsl:template>

  <xsl:template match="w:pict">
    <BlockUIContainer>
      <Viewbox>
        <xsl:apply-templates select="*"/>
      </Viewbox>
    </BlockUIContainer>
  </xsl:template>

  <xsl:template match="w:txbxContent">
    <FlowDocument PagePadding="5">
      <xsl:apply-templates select="*"/>
    </FlowDocument>
  </xsl:template>

  <xsl:template match="w:br">
    <LineBreak/>
  </xsl:template>

  <xsl:template match="w:p[w:pPr/w:pStyle/@w:val='ListParagraph' and preceding-sibling::w:p[1]/w:pPr/w:pStyle/@w:val='ListParagraph']" priority="1">
    <xsl:comment>Following List Paragraph</xsl:comment>
  </xsl:template>

  <xsl:variable name="ListMarkers">
    <TextMarker>Decimal</TextMarker>
    <TextMarker>LowerLatin</TextMarker>
    <TextMarker>LowerRoman</TextMarker>
  </xsl:variable>

  <xsl:template match="w:p[w:pPr/w:pStyle/@w:val='ListParagraph']" priority="0">
    <xsl:param name="OutlineLevel" select="w:pPr/w:numPr/w:ilvl/@w:val"/>

    <xsl:call-template name="GenerateList">
      <xsl:with-param name="outlineLevel" select="$OutlineLevel"/>
      <xsl:with-param name="items" select="following-sibling::*"/>
    </xsl:call-template>

    <!--
    <xsl:variable name="styleId" select="w:pPr/w:pStyle/@w:val"/>
    <xsl:comment>
      List with Outline Level <xsl:value-of select="number($OutlineLevel)"/>: <xsl:value-of select="msxsl:node-set($ListMarkers)/node()[number($OutlineLevel) + 1]"/>
    </xsl:comment>
    <List Style="{{StaticResource {$styleId}}}" MarkerStyle="{msxsl:node-set($ListMarkers)/node()[number($OutlineLevel) + 1]}">
      <xsl:call-template name="ListItem">
        <xsl:with-param name="item" select="."/>
        <xsl:with-param name="outlineLevel" select="number($OutlineLevel) + 1"/>
      </xsl:call-template>
      <xsl:comment>Other list items at the same level:</xsl:comment>
      <xsl:variable name="level" select="w:pPr/w:numPr/w:ilvl/@w:val"/>
      -->
    <!--
      <xsl:apply-templates select="following-sibling::w:p[w:pPr/w:pStyle/@w:val='ListParagraph' and w:pPr/w:numPr/w:ilvl/@w:val=$level]" mode="ListItem"/> 
      -->
    <!--</List>-->
  </xsl:template>

  <xsl:template name="GenerateList">
    <xsl:param name="items"/>
    <xsl:param name="outlineLevel"/>
    <xsl:comment>
      Generating list of <xsl:value-of select ="count($items)"/> at level <xsl:value-of select="$outlineLevel"/>
    </xsl:comment>
    <xsl:variable name="styleId" select="w:pPr/w:pStyle/@w:val"/>
    <List MarkerStyle="{msxsl:node-set($ListMarkers)/node()[number($outlineLevel) + 1]}">
      <xsl:call-template name="ListItem">
        <xsl:with-param name="item" select="."/>
        <xsl:with-param name="outlineLevel" select="number($outlineLevel) + 1"/>
      </xsl:call-template>
      <xsl:comment>Other list items at the same level:</xsl:comment>
      <xsl:variable name="level" select="w:pPr/w:numPr/w:ilvl/@w:val"/>
      <xsl:if test="count($items) &gt; 1">
        <xsl:call-template name="GenerateList">
          <xsl:with-param name="items" select="$items/following-sibling::*"/>
          <xsl:with-param name="outlineLevel" select="$outlineLevel + 1"/>
        </xsl:call-template>
      </xsl:if>
    </List>
  </xsl:template>

  <xsl:template match="w:p" mode="ListItem">
    <xsl:param name="outlineLevel"/>
    <xsl:call-template name="ListItem">
      <xsl:with-param name="item" select="."/>
      <xsl:with-param name="outlineLevel" select="$outlineLevel"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="ListItem">
    <xsl:param name="item"/>
    <xsl:param name="outlineLevel"/>
    <ListItem>
      <Paragraph>
        <xsl:variable name="styleId" select="$item/w:pPr/w:pStyle/@w:val"/>
        <xsl:variable name="style" select="//w:style[@w:styleId=$styleId]"/>
        <xsl:call-template name="Style">
          <xsl:with-param name="style" select="$style"/>
        </xsl:call-template>
        <xsl:apply-templates select="$item/*"/>
      </Paragraph>
      <xsl:variable name="followingItems" select="$item/following-sibling::w:p"/>
      <xsl:comment>
        <xsl:value-of select="count($followingItems)"/> followers
      </xsl:comment>
      <xsl:call-template name="List">
        <xsl:with-param name="items" select="$followingItems"/>
        <xsl:with-param name="outlineLevel" select="$outlineLevel"/>
      </xsl:call-template>
    </ListItem>
    <xsl:variable name="following" select="following::w:p"/>
    <xsl:if test="$following[1]/w:pPr/w:pStyle/w:val='ListParagraph'">
      <xsl:call-template name="ListItem">
        <xsl:with-param name="item" select="$following"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="List">
    <xsl:param name="items"/>
    <xsl:param name="outlineLevel"/>
    <xsl:comment>
      List at level <xsl:value-of select="$outlineLevel"/>
    </xsl:comment>
    <xsl:variable name="firstItem" select="$items[1]"/>
    <xsl:comment>
      First Item:
      <xsl:value-of select ="$firstItem"/>
    </xsl:comment>
    <xsl:if test="$firstItem/w:pPr/w:pStyle/@w:val='ListParagraph' and $firstItem/w:pPr/w:numPr/w:ilvl/@w:val=$outlineLevel">
      <xsl:variable name="styleId" select="$firstItem/w:pPr/w:pStyle/@w:val"/>
      <List Style="{{StaticResource {$styleId}}}" MarkerStyle="{msxsl:node-set($ListMarkers)/node()[number($outlineLevel) + 1]}">
        <xsl:variable name="level" select="w:pPr/w:numPr/w:ilvl/@w:val"/>
        <xsl:comment>Nested List items</xsl:comment>
        <xsl:call-template name="NestedListItems">
          <xsl:with-param name="items" select="$firstItem/following-sibling::w:p"/>
          <xsl:with-param name="outlineLevel" select="$outlineLevel"/>
        </xsl:call-template>
        <!--<xsl:apply-templates select="$firstItem/following-sibling::w:p[w:pPr/w:pStyle/@w:val='ListParagraph' and w:pPr/w:numPr/w:ilvl/@w:val=$level]" mode="ListItem"/>-->
      </List>
    </xsl:if>
  </xsl:template>

  <xsl:template name="NestedListItems">
    <xsl:param name="items"/>
    <xsl:param name="outlineLevel"/>
    <xsl:variable name="firstItem" select="$items[1]"/>
    <xsl:comment>
      <xsl:value-of select="local-name($firstItem)"/>
      <xsl:value-of select="$outlineLevel"/>
    </xsl:comment>
    <xsl:if test="$firstItem/w:pPr/w:pStyle/@w:val='ListParagraph' and $firstItem/w:pPr/w:numPr/w:ilvl/@w:val=$outlineLevel">
      <xsl:comment>
        Nested List Item: <xsl:value-of select="$firstItem"/>
      </xsl:comment>
    </xsl:if>
  </xsl:template>

  <xsl:template match="w:r[w:instrText]"/>

  <!-- These are page numbers in a Table of contents. -->
  <xsl:template match="w:r[w:rPr/w:webHidden]">
    <xsl:comment>Web Hidden Run</xsl:comment>
  </xsl:template>

  <xsl:template match="w:hyperlink[@w:anchor]">
    <xsl:comment>
      Hyperlink to <xsl:value-of select="@w:anchor"/>
    </xsl:comment>
    <Hyperlink>
      <xsl:attribute name="NavigateUri">
        <xsl:value-of select="concat('#', generate-id(@w:anchor))"/>
      </xsl:attribute>
      <xsl:apply-templates select="*"/>
    </Hyperlink>
  </xsl:template>

  <xsl:template match="w:hyperlink[@r:id]">
    <xsl:variable name="instrText">
      <xsl:for-each select="w:r/w:instrText | w:ins/preceding-sibling::w:ins/w:r/w:instrText">
        <xsl:value-of select="."/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="hyperlink">
      <xsl:variable name="id" select="@r:id"/>
      <xsl:value-of select="//rels:Relationship[@Id=$id and @Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink']/@Target"/>
    </xsl:variable>
    <xsl:comment>
      <xsl:value-of select="$instrText"/>
    </xsl:comment>
    <Hyperlink NavigateUri="{$hyperlink}" TargetName="_blank">
      <xsl:apply-templates select="*"/>
    </Hyperlink>
  </xsl:template>

  <xsl:template match="w:r[w:fldChar]">
    <xsl:comment>Field</xsl:comment>
  </xsl:template>

  <xsl:template match="w:instrText">
    <xsl:comment>TOC Page Number - not relevant for WPF</xsl:comment>
  </xsl:template>

  <xsl:template match="w10:anchorlock">
    <xsl:comment>anchorlock</xsl:comment>
  </xsl:template>

  <!-- Embedded Image -->
  <xsl:template match="w:drawing[wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed]">
    <xsl:variable name="id" select="wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed"/>
    <xsl:variable name="target" select="//rels:Relationship[@Id=$id and @Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/image']/@Target"/>
    <xsl:variable name="packageName" select="concat('/word/', $target)"/>
    <xsl:variable name="data" select="string(//pkg:part[@pkg:name=$packageName]/pkg:binaryData)"/>
    <xsl:comment>
      Embedded Image: <xsl:value-of select="$id"/>,
      Package Name: <xsl:value-of select="$packageName"/>
    </xsl:comment>

    <xsl:variable name="source" select="imaging:SaveImage($target, $data)"/>

    <xsl:if test="string-length($source)">
      <Image Source="{$source}">
        <xsl:if test="wp:inline/wp:extent/@cx">
          <xsl:attribute name="Width">
            <xsl:value-of select="wp:inline/wp:extent/@cx div 9525"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="wp:inline/wp:extent/@cy">
          <xsl:attribute name="Height">
            <xsl:value-of select="wp:inline/wp:extent/@cy div 9525"/>
          </xsl:attribute>
        </xsl:if>
      </Image>
    </xsl:if>
  </xsl:template>

  <!-- Linked Image -->
  <xsl:template match="w:drawing[wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:link]">
    <xsl:variable name="id" select="wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:link"/>
    <xsl:variable name="target" select="//rels:Relationship[@Id=$id and @Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/image']/@Target"/>

    <xsl:variable name="source" select="imaging:CopyImage($target)"/>

    <xsl:comment>
      Linked Image: <xsl:value-of select="$target"/>,
    </xsl:comment>

    <xsl:if test="string-length($source)">
      <Image Source="{$source}"/>
    </xsl:if>
  </xsl:template>


  <xsl:template name="TextStyle">
    <xsl:param name="style"/>
    <xsl:variable name="elementName">
      <xsl:choose>
        <xsl:when test="local-name($style) = 'i'">
          <xsl:text>Italic</xsl:text>
        </xsl:when>
        <xsl:when test="local-name($style) = 'b'">
          <xsl:text>Bold</xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="string-length($elementName)">
        <xsl:element name="{$elementName}">
          <xsl:call-template name="TextStyle">
            <xsl:with-param name="style" select="$style/following-sibling::node()[1]"/>
          </xsl:call-template>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="w:t">
    <Run Text="{util:CleanXaml(text())}">
      <xsl:if test="parent::node()/w:rPr/w:rStyle/@w:val">
        <xsl:attribute name="Style">
          {<xsl:value-of select="concat('StaticResource ', parent::node()/w:rPr/w:rStyle/@w:val)"/>}
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="parent::node()/w:rPr/w:color/@w:val">
        <xsl:attribute name="Foreground">
          <xsl:value-of select="concat('#', parent::node()/w:rPr/w:color/@w:val)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="parent::node()/w:rPr/w:highlight/@w:val">
        <xsl:attribute name="Background">
          <xsl:value-of select="parent::node()/w:rPr/w:highlight/@w:val"/>
        </xsl:attribute>
      </xsl:if>
    </Run>
    <xsl:text/>
  </xsl:template>

  <!-- Table -->
  <xsl:template match="w:tbl">
    <Table>
      <xsl:apply-templates select="w:tblPr/w:tblBorders"/>
      <xsl:apply-templates select="w:tblPr/w:tblStyle/@w:val" mode="Style"/>
      <xsl:apply-templates select="w:tblGrid"/>
      <TableRowGroup>
        <xsl:apply-templates select="w:tr">
          <xsl:with-param name="styleName" select="w:tblPr/w:tblStyle/@w:val"/>
        </xsl:apply-templates>
      </TableRowGroup>
    </Table>
  </xsl:template>

  <xsl:template match="w:tblBorders">
    <xsl:attribute name="BorderBrush">Black</xsl:attribute>
    <xsl:attribute name="BorderThickness">
      <xsl:choose>
        <xsl:when test="w:left">
          <xsl:apply-templates select="w:left" mode="Thickness"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
      <xsl:text>,</xsl:text>
      <xsl:choose>
        <xsl:when test="w:top">
          <xsl:apply-templates select="w:top" mode="Thickness"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
      <xsl:text>,</xsl:text>
      <xsl:choose>
        <xsl:when test="w:right">
          <xsl:apply-templates select="w:right" mode="Thickness"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
      <xsl:text>,</xsl:text>
      <xsl:choose>
        <xsl:when test="w:bottom">
          <xsl:apply-templates select="w:bottom" mode="Thickness"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="*" mode="Thickness">
    <xsl:choose>
      <xsl:when test="@w:val='none'">0</xsl:when>
      <xsl:when test="@w:val='single'">
        <xsl:value-of select="@w:sz div 20"/>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="w:tblGrid">
    <Table.Columns>
      <xsl:for-each select="w:gridCol">
        <!--<TableColumn Width="{@w:w div 20}pt"/>-->
        <TableColumn/>
      </xsl:for-each>
    </Table.Columns>
  </xsl:template>

  <xsl:template match="w:tr">
    <xsl:param name="styleName"/>
    <TableRow>
      <xsl:apply-templates select="w:tc">
        <xsl:with-param name="styleName" select="$styleName"/>
      </xsl:apply-templates>
    </TableRow>
  </xsl:template>

  <xsl:template match="w:tc">
    <xsl:param name="styleName"/>
    <xsl:variable name="style" select="//w:style[@w:type='table' and @w:styleId=$styleName]"/>

    <TableCell>
      <xsl:apply-templates select="w:p"/>
    </TableCell>
  </xsl:template>

</xsl:stylesheet>
